﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;

namespace DLib
{

    class Files
    {
        /*
         * A few useful file accessing methods.
         *      getFileAsString(filename)
         *      writeStringToFile(filename,string);
         *      WriteToFile(filename,string[])
         */

        public static string getFileAsString(string fileName)
        {
            try
            {
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sReader = new StreamReader(fileStream, Encoding.GetEncoding(1252)))
                    {
                        string contents = sReader.ReadToEnd();
                        return contents;
                    }
                }
            }
            catch (Exception ex)
            {
                return "Problem Reading File\t"+ex.Message;
            }

        }
        public static string[] getFileAsArray(string fileName)
        {
            return Regex.Split(getFileAsString(fileName), Environment.NewLine);
        }
        public static void WriteToFile(string fileName,string input)
        {



            StreamWriter sWriter = null;

            string contents = input;
            try
            {
                FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Write);
                sWriter = new StreamWriter(fileStream);
                sWriter.Write(contents);
            }
            catch (Exception e)
            {
                Console.Write("Can't find that file, dumbass!\\n");
                Console.Write(e.Message);

            }


            finally
            {
                if (sWriter != null)
                {
                    sWriter.Close();
                }
            }

        }
        public static void WriteToFile(string fileName, string[] input)
        {
            StreamWriter sWriter = null;
            try
            {
                FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Write);
                sWriter = new StreamWriter(fileStream);
                foreach (string strLine in input)
                {
                    sWriter.WriteLine(strLine);
                }
            }
            catch (Exception e)
            {
                Console.Write("Can't find that file, dumbass!\\n");
                Console.Write(e.Message);

            }
            finally
            {
                if (sWriter != null)
                {
                    sWriter.Close();
                }
            }

        }
        public static string getXmlFileAsString(string fileName)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
            xmlTextWriter.Formatting = Formatting.Indented;
            xmlDocument.WriteTo(xmlTextWriter);
            xmlTextWriter.Flush();
            return stringWriter.ToString();
        }
    }


}// end of DLib Namespace

